<?php
if (isset($InputData["user_name"]) && isset($InputData["user_password"])) {
    if ($InputData["user_password"] == '') exit('登录密码不能为空!');
    //登陆事件
    //20170128 添加 如果为管理员登陆 用户必须为管理员身份 其它身份都算登陆失败
    //检查是否管理员登陆的处理，由数据模型user_local_login函数处理
    //连接用户数据模型
    require_once("{$CfgPara->root}/model/user.m.php");
    $myUser = new user_m($CfgPara->database);
    $result = $myUser->user_local_login($InputData["user_name"], $InputData["user_password"]);
    if (isset($result['Id'])) {
        $_SESSION["user_id"] = $result['Id'];
        $_SESSION["user_name"] = $result['Name'];
        $_SESSION["user_level"] = $result['Level'];
        $_SESSION['user_avatar'] = $result['Avatar'];
        if (isset($InputData['user_login_local_auto']) && $InputData['user_login_local_auto'] == 1) {
            $session_id = session_id();
            // 如果为自动登陆 保存此对话信息一周
            setcookie('PHPSESSID', $session_id, time() + 7 * 24 * 3600, '/');
        }
        echo 1;
    } else {
        echo($result);
    }
} elseif (isset($InputData['user_login_third_auto'])) {
    echo '第三方账户接口尚未开通';
    goto page_end;
    //设置第三方自动登陆后处理
    $_SESSION['user_login_third_auto'] = $InputData['user_login_third_auto'];
    echo(1);
    exit();
} elseif (isset($_SESSION['weibo_token']) && isset($user_weibo_message) && !isset($InputData['user_weibo_name'])) {
    echo '第三方账户接口尚未开通';
    goto page_end;
    //微博用户登陆
    require_once("{$CfgPara->root}/model/user.m.php");
    $myUser = new user_m($CfgPara->database);
    $weiboLoginState = $myUser->user_weibo_login($user_weibo_message);
    if (isset($weiboLoginState['Id'])) {
        $_SESSION["user_id"] = $weiboLoginState['Id'];
        $_SESSION["user_name"] = $weiboLoginState['Name'];
        $_SESSION["user_level"] = 10;
        $_SESSION['user_avatar'] = $weiboLoginState['Avatar'];
        $_SESSION['weibo_user'] = $_SESSION['weibo_token']['uid'];
        if (isset($_SESSION['user_login_third_auto']) && $_SESSION['user_login_third_auto'] == 1) {
            $session_id = session_id();
            setcookie('PHPSESSID', $session_id, time() + 7 * 24 * 3600, '/');
        }
    }
    // 如果用户在本地从未在本站登陆过 将生成需要输入用户名的注册视图
    require("{$CfgPara->root}/view/user_login_weibo_callback.app.v.php");
} elseif (isset($_SESSION['weibo_token']) && isset($InputData['user_weibo_name'])) {
    echo '第三方账户接口尚未开通';
    goto page_end;
    //微博用户填写本站用户名后提交
    require_once("{$CfgPara->root}/model/user.m.php");
    $myUser = new user_m($CfgPara->database);
    require_once("{$CfgPara->root}/sina_weibo/config.php");
    require_once("{$CfgPara->root}/sina_weibo/saetv2.ex.class.php");
    $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['weibo_token']['access_token']);
    //用户名提交后 重新获取微博用户信息
    $user_weibo_message = $c->show_user_by_id($_SESSION['weibo_token']['uid']);
    if (isset($user_weibo_message['id'])) {
        $result = $myUser->user_reg($InputData['user_weibo_name'], '', $user_weibo_message);
        if ($result == 1) {
            $userMsg = $myUser->user_msg('Name', $InputData['user_weibo_name']);
            if (is_string($userMsg)) {
                echo($userMsg);
                exit();
            }
            $_SESSION["user_id"] = $userMsg->Id;
            $_SESSION["user_name"] = $userMsg->Name;
            $_SESSION["user_level"] = 10;
            $_SESSION['user_avatar'] = $userMsg->Avatar;
            $_SESSION['weibo_user'] = $_SESSION['weibo_token']['uid'];
            if (isset($_SESSION['user_login_third_auto']) && $_SESSION['user_login_third_auto'] == 1) {
                $session_id = session_id();
                setcookie('PHPSESSID', $session_id, time() + 7 * 24 * 3600);
            }
        }
        echo($result);
        exit();
    } else {
        exit('获取微博用户信息失败');
    }
} elseif (isset($InputData["user_logout"]) && $InputData["user_logout"] == 1) {
    //用户登出处理
    if (isset($_SESSION['weibo_user'])) {
        $result = 2; //微博用户 通知浏览器退出微博
    } else {
        $result = 1; //本地用户
    }
    //用户登出
    session_unset();
    session_destroy();
    echo($result);
    exit();
} else {
    //进入登陆界面
    if ($InputData['controller'] == 'shoe_show_edit' ||
        (isset($InputData['login']) && $InputData['login'] == 'shoe_show' && isset ($InputData['work_id']))
    ) {
        $login_type = '2'; //登陆类型 1或‘’为普通登陆 2为鞋主秀登陆 3为管理员登陆
        $login_para = $InputData['work_id'];
    } else {
        $login_type = '1';
        $login_para = '0';
    }
    //微博登陆授权页面
    require_once("{$CfgPara->root}/sina_weibo/config.php");
    require_once("{$CfgPara->root}/sina_weibo/saetv2.ex.class.php");
    $weibo_o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
    $weibo_code_url = $weibo_o->getAuthorizeURL(WB_CALLBACK_URL);
    //20170128 添加 如果是普通登陆 则注销掉管理员登陆的对话$_SESSION['user_admin']
    if (!isset($InputData['login']) || $InputData['login'] != 'admin') {
        if (isset($_SESSION['user_admin'])) {
            unset($_SESSION['user_admin']);
        }
    } else {
        $login_type = '3'; //如果为管理员登陆 $loginType为3
        $login_para = '0';
    }
    //获取登录页面
    require("{$viewRoot}user_login{$viewEx}");
}

page_end:;
